package com.hzhc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysUser;
import com.hzhc.entity.VideoPointInfo;
import com.hzhc.mapper.SysUserMapper;
import com.hzhc.mapper.VideoPointInfoMapper;
import com.hzhc.service.VideoPointInfoService;
import com.hzhc.vo.VideoPointInfoVoVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 监控点信息 服务实现类
 * </p>
 *
 * @author oyd
 * @since 2023-12-15
 */
@Service
@RequiredArgsConstructor
public class VideoPointInfoServiceImpl extends ServiceImpl<VideoPointInfoMapper, VideoPointInfo> implements VideoPointInfoService {

    private final SysUserMapper sysUserMapper;

    @Override
    public List<VideoPointInfo> getListByUserId(Long userId, VideoPointInfo vo) {
        SysUser sysUser = sysUserMapper.selectById(userId);
        String videoPoints = sysUser.getVideoPoints();
        LambdaQueryWrapper<VideoPointInfo> queryWrapper = Wrappers.<VideoPointInfo>lambdaQuery()
                .eq(VideoPointInfo::getDisable, false)
                .eq(vo.getType() != null, VideoPointInfo::getType, vo.getType())
                .orderByAsc(VideoPointInfo::getSort)
                ;
        if (StrUtil.isNotBlank(videoPoints)) {
            queryWrapper.in(StrUtil.isNotBlank(videoPoints), VideoPointInfo::getId, videoPoints.split(","));
        }
        List<VideoPointInfo> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<VideoPointInfoVoVo> getSnapCameraJson(VideoPointInfoVoVo vo) {
        List<VideoPointInfoVoVo> videoPointInfoList = new ArrayList<VideoPointInfoVoVo>();
        List<VideoPointInfo> list = baseMapper.selectList(Wrappers.<VideoPointInfo>lambdaQuery()
                .eq(VideoPointInfo::getDisable, false)
                .like(StringUtils.isNotBlank(vo.getIpaddress()), VideoPointInfo::getIpaddress, vo.getIpaddress())
                .like(StringUtils.isNotBlank(vo.getPort()), VideoPointInfo::getPort, vo.getPort())
                .in(vo.getTypes() != null && vo.getTypes().length > 0, VideoPointInfo::getType, vo.getTypes())
        );
        for (VideoPointInfo videoPointInfo : list) {
            VideoPointInfoVoVo videoPointInfoVoVo = new VideoPointInfoVoVo();
            BeanUtil.copyProperties(videoPointInfo, videoPointInfoVoVo);
            videoPointInfoVoVo.setIpaddressPort(videoPointInfoVoVo.getIpaddress() + ":" + videoPointInfoVoVo.getPort());
            videoPointInfoVoVo.setUsername(null).setPassword(null);
            videoPointInfoList.add(videoPointInfoVoVo);
        }
        return videoPointInfoList;
    }
}
